package regextest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {
    public static void main(String[] args) {
        String str = "hello , java!";
        System.out.println(str.replaceFirst("\\w*", "♦"));
        System.out.println(str.replaceFirst("\\w*?", "♦"));

        String str2 = "我的电话是13812329988，你的电话是多少？15899776688吗？" +
                "他的电话是13899885522";
        //抓取13X和15X段的手机号
        Pattern p = Pattern.compile("((13\\d)|(15\\d))\\d{8}");
        Matcher m = p.matcher(str2);
        while (m.find()) {
            System.out.println(m.group() + "  起始位置：" + m.start() + "  结束位置：" + m.end());
        }

        matchesTest();
    }

    public static void matchesTest() {
        String[] mails = {
                "kongyeeku@163.com",
                "kongyeeku@gmail.com",
                "ligang@crazyit.org",
                "wawa@abc.xx"
        };
        String mailRegex = "\\w{3,20}@\\w+\\.(com|org|cn|net|gov)";
        Pattern mailPattern = Pattern.compile(mailRegex);
        Matcher matcher = null;
        for (String mail : mails) {
            if (matcher == null) {
                matcher = mailPattern.matcher(mail);
            }else {
                matcher.reset(mail);
            }
            System.out.println(mail+(matcher.matches()?"是":"不是")+"一个有效的邮件地址");
        }
    }
}
